/*
** fork.c for Fork in /home/vriely/Dropbox/work/Unix/Zappy_rendu/serv-src
**
** Made by Samuel Vrielynck
** Login   <vriely_s@epitech.net>
**
** Started on  Thu Jul 05 14:40:43 2012 Samuel Vrielynck
** Last update Wed Jul 18 13:12:15 2012 Samuel Vrielynck
*/

#include	<stdlib.h>

#include	"server.h"

static void	send_data(t_game *game, t_fork *fork)
{
  if (game->graphic != NULL)
    {
      game->graphic->send->concat(game->graphic->send, "enw ");
      game->graphic->send->concat_value(game->graphic->send, fork->id);
      game->graphic->send->concat(game->graphic->send, " ");
      game->graphic->send->concat_value(game->graphic->send, \
					fork->client->sock);
      game->graphic->send->concat(game->graphic->send, " ");
      game->graphic->send->concat_value(game->graphic->send, fork->coord.x);
      game->graphic->send->concat(game->graphic->send, " ");
      game->graphic->send->concat_value(game->graphic->send, fork->coord.y);
      game->graphic->send->concat(game->graphic->send, "\n");
    }
}

char		handle_fork(t_event *event, t_game *game, t_list *clients,
    long unsigned int elapsed_time)
{
  t_fork	*ffork;

  ffork = (struct s_fork *)event->data;
  (void)clients;
  if (ffork->status == -1)
  {
    if (ffork->client && ffork->client->gamer &&
	is_alive(game->time, *(ffork->client->gamer), elapsed_time))
    {
      ffork->client->gamer->team->egg->add_back(\
	ffork->client->gamer->team->egg, \
	new_egg(ffork->id, ffork->coord, game->time));
      send_data(game, ffork);
    }
  }
  free(ffork);
  return (1);
}
